package TUILayer;

import ModelLayer.*;
import ControlLayer.*;
import java.util.*;
/**
 * Write a description of class MenuDVDs here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MenuDVDs
{
    Scanner keyboard = new Scanner(System.in);
    public MenuDVDs()
    {
        DVDControls dvdCont = new DVDControls();
        dvdCont.createObjects();
    }
    
    public void dvdsMenu()
    {
        boolean back = false;
        while(!back)
        {
            int choiseDVDs = writeDVDs();
            if(choiseDVDs == 1)
            {
                addDVDs();
            }
            else
            {
                if(choiseDVDs == 2)
                {
                    deleteDVDs();
                }
                else
                {
                    if(choiseDVDs == 3)
                    {
                        updateDVDs();
                    }
                    else
                    {
                        if(choiseDVDs == 4)
                        {
                            showDVDs();
                        }
                        else
                        {
                            if(choiseDVDs == 5)
                            {
                                createCopies();
                            }
                            else
                            {
                                if(choiseDVDs == 6)
                                {
                                    back = true;
                                }
                                else
                                {
                                    System.out.println("Command not valid");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    private int writeDVDs()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" ");
        System.out.println("============================");
        System.out.println("|       DVDS SELECTION     |");
        System.out.println("============================");
        System.out.println("| Options:                 |");
        System.out.println("|        1. Add DVDs       |");
        System.out.println("|        2. Delete DVDs    |");
        System.out.println("|        3. Update DVDs    |");
        System.out.println("|        4. Show DVDs      |");
        System.out.println("|        5. Create Copy    |");
        System.out.println("|        6. Back           |");
        System.out.println("============================");
        System.out.println("     Make your choise: "     );
        
        int choiseDVDs = keyboard.nextInt();
        
        return choiseDVDs;
    }
    
    private void addDVDs()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            DVDControls dvdControl = new DVDControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input DVD's barcode(6 symbols): ");
                String barcode = keyboard.nextLine();
                if(barcode.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Barcode MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    System.out.println("Input DVD's title: ");
                    String title = keyboard.nextLine();
                    System.out.println("Input DVD's artist: ");
                    String artist = keyboard.nextLine();
                    System.out.println("Input DVD's publication date(format: dd.MM.yyyy): ");
                    String publicationDate = keyboard.nextLine();
                
                    DatesControls datesControl = new DatesControls();
                    datesControl.isValid(publicationDate);
                    
                    if(datesControl.resutValid().equals("Date valid."))
                    {
                        dvdControl.createDVDs(barcode, title, artist, publicationDate);
                        System.out.println(" ");
                        System.out.println(dvdControl.addResult());
                        end = true;
                    }
                    else
                    {
                        System.out.println(" ");
                        System.out.println("Publication date NOT VALID!");
                        end = true;
                    }
                }
            }
        }
    }
    
    private void deleteDVDs()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            DVDControls dvdControl = new DVDControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input DVD's barcode(6 symbols): ");
                String barcode = keyboard.nextLine();
                if(barcode.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Barcode MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    dvdControl.deleteDVDs(barcode);
                    System.out.println(" ");
                    System.out.println(dvdControl.returnResult());
                    end = true;
                }
            }
        }
    }
    
    private void updateDVDs()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            DVDControls dvdControl = new DVDControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input DVD's  OLD barcode(6 symbols): ");
                String barcode = keyboard.nextLine();
                if(barcode.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Barcode MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    System.out.println("Input DVD's title: ");
                    String newTitle = keyboard.nextLine();
                    System.out.println("Input DVD's artist: ");
                    String newArtist = keyboard.nextLine();
                    System.out.println("Input DVD's publication date(format: dd.MM.yyyy): ");
                    String newPublicationDate = keyboard.nextLine();
                    
                    DatesControls datesControl = new DatesControls();
                    datesControl.isValid(newPublicationDate);
                    
                    if(datesControl.resutValid().equals("Date valid."))
                    {
                        dvdControl.updateDVDs(barcode, newTitle, newArtist, newPublicationDate);
                        System.out.println();
                        System.out.println("DVD updated.");
                        end = true;
                    }
                    else
                    {
                        System.out.println(" ");
                        System.out.println("Publication date NOT VALID!");
                        end = true;
                    }
                }   
            }
        }
    }
    
    private void showDVDs()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            DVDControls dvdControl = new DVDControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input DVD's barcode(6 symbols): ");
                String barcode = keyboard.nextLine();
                if(barcode.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Barcode MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    dvdControl.showDVDs(barcode);
                    System.out.println(" ");
                    System.out.println(dvdControl.showDVDs(barcode));
                    end = true;
                }
            }
        }        
    }
    
    private void createCopies()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            DVDControls dvdControl = new DVDControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                if(dvdControl.sizeOfDVDList() < 1)
                {
                    System.out.println(" ");
                    System.out.println("No available DVDs");
                    end = true;
                }
                else
                {
                    System.out.println("Input DVD's barcode(6 symbols): ");
                    String barcode = keyboard.nextLine();
                    if(barcode.length() != 6)
                    {
                        System.out.println(" ");
                        System.out.println("Barcode MUST BE 6 symbols long!");
                        end = true;
                    }
                    else
                    {
                        System.out.println("Input copy's serial number(6 symbols): ");
                        String serialNumber = keyboard.nextLine();
                        if(serialNumber.length() != 6)
                        {
                            System.out.println(" ");
                            System.out.println("Serial number MUST BE 6 symbols long!");
                            end = true;
                        }
                        else
                        {
                            System.out.println("Input copy's purchase date(format: dd.MM.yyyy): ");
                            String purchaseDate = keyboard.nextLine();
                            
                            DatesControls datesControl = new DatesControls();
                            datesControl.isValid(purchaseDate);
                    
                            if(datesControl.resutValid().equals("Date valid."))
                            {
                                System.out.println("Input copy's purchase price: ");
                                String purchasePrice = keyboard.nextLine();
                                
                                dvdControl.createCopies(barcode, serialNumber, purchaseDate, purchasePrice);
                                System.out.println(" ");
                                System.out.println(dvdControl.resultCreateCopies());
                                end = true;
                            }
                            else
                            {
                                System.out.println(" ");
                                System.out.println("Purchase date NOT VALID!"); 
                                end = true;    
                            }
                        }
                    }
                }
            }
        }
    }
}
